
PROCESS_AJAX = null;
NUMBER_PROCESS = 0;
$(document).ready(function(){
    if(CORE.isLogin){PROCESS_AJAX = setInterval(function(){$.process();},1000);}
});
(function( $ ){
    $.process = function(){
        NUMBER_PROCESS += 1;
        $.post('~chat/chatMessage',{},function(data){
            if(data !='')
            {
                var dataprocess = JSON.parse(data);
                var aData = data.split('*&*');
                if(dataprocess.message.length != 0)
                {
                    var aMessage = aData[0].split();
                    $.each(dataprocess.message , function(index,value){
                        var mes = new Array();
                        mes = value;
                        var user = mes[0];
                        var content = mes[1];
                        var time = mes[2];
                        var id = mes[3];
                        if($('#chat_'+user+' .list_user_chat').length> 0)
                        {
                            $.post('~chat/updateMessageAjax',{id : id},function(data){});
                            var sHtml = '<li class="friend_message" title="'+time+'">' + content + "</li>";
                            $('#chat_'+user+' .list_user_chat').append(sHtml);
                            $('#chat_'+user+' .list_user_chat').scrollTop($('#chat_'+user+' .list_user_chat')[0].scrollHeight);
                            $('#img_'+user).show();
                            var isHidden = $("#chat_"+user).is( ":hidden" );
                            if(isHidden )
                            {
                                $.creatScreenChat(user);
                            }
                        }
                        else
                        {
                            $.creatScreenChat(user);
                        }
                    });
                }
                if(dataprocess.onlineUser.length != 0)
                {
                    $('.message_area #list_friend_inline').empty();
                    $('#tb_limit_div').empty();
                    $.each(dataprocess.onlineUser , function(index,value){
                        var user = new Array();
                        user = value;
                        if(user.length != 0)
                        {
                            $('.message_area #list_friend_inline').append(user[0]);
                            $('#tb_limit_div').append(user[1]);
                        }
                    });
                }
                if(dataprocess.numberNotification != 0)
                {
                    // Get count notification
                    $('.count_notification').css('display','block');
                    $('.count_notification').text(dataprocess.numberNotification);
                }
                else{
                    $('.count_notification').css('display','none');
                }
                if(dataprocess.poupNotification.length != 0)
                {
                    $('.poup_class_notification').empty();
                    $.each(dataprocess.poupNotification , function(index,value){
                        $('.poup_class_notification').append(value);
                        var isHidden = $( ".poup_class_notification .poup_notification" ).is( ":visible" );
                        if(isHidden == false)
                        {
                            $( ".poup_class_notification .poup_notification" ).fadeIn(1000, function() {
                                // Animation complete.
                            });
                        }
                    });
                }
            }
        });
    }
})( jQuery );